package com.imy.rootserver.gesture;

import android.view.InputEvent;

import com.imy.rootserver.exception.UiAutomator2Exception;

import java.lang.reflect.Method;

import static com.imy.rootserver.utils.ReflectionUtils.invoke;
import static com.imy.rootserver.utils.ReflectionUtils.method;

/**
 * Created by Administrator on 2019/4/10/010.
 */

public class InteractionController {
    private static final String CLASS_INTERACTION_CONTROLLER = "androidx.test.uiautomator.InteractionController";
    private static final String METHOD_INJECT_EVENT_SYNC = "injectEventSync";
    private static final String METHOD_CLICK = "clickNoSync";
    private final Object interactionController;
    private Method injectEvent;
    private String TAG = "InteractionController";

    public InteractionController(Object interactionController) {
        this.interactionController = interactionController;
    }

    public boolean injectEventSync(final InputEvent event) throws UiAutomator2Exception {

        if (injectEvent == null) {
            injectEvent = method(CLASS_INTERACTION_CONTROLLER,
                    METHOD_INJECT_EVENT_SYNC, InputEvent.class);
        }
        Boolean invoke = (Boolean) invoke(injectEvent, interactionController, event);
        return invoke;
    }

    public boolean click(int x, int y) {
        //先拿到方法
        Object invoke = invoke(method(CLASS_INTERACTION_CONTROLLER, METHOD_CLICK, int.class, int.class), interactionController, x, y);
        return (boolean) invoke;
    }

    public boolean touchDown(int x, int y) {
        return false;
    }
}
